<!--
 * @FileDescription 视角
 * @Author lz
 * @Date 20230530 11:45:10
 * @LastEditors lz
 * @LastEditTime 20230530 11:45:10
-->
<template>
  <div class="viewBox" v-if="showDialog">
    <div class="controlMap">
      <img src="/public/images/ic_menu_展开.png" @click="unShowMap" />
      <div>导航视图</div>
    </div>

    <div class="map" @mouseenter="mouseenter" @mouseleave="mouseleave">
      <!-- <div class="BlackSpot" v-for="(item, index) in HomeContent.Default.ViewData.value" @click="SwitchView(item.Id)">
            <a-button>{{ item.Name }}</a-button>
        </div> -->
      <!-- <div class="ap drop1" @click="SwitchView(HomeContent.Default.ViewData.value[0].Id)"
            :class="state.current == 1 ? 'current' : ''"></div>
        <div class="ap drop2" @click="SwitchView(HomeContent.Default.ViewData.value[1].Id)"
            :class="state.current == 2 ? 'current' : ''"></div>
        <div class="ap drop3" @click="SwitchView(HomeContent.Default.ViewData.value[2].Id)"
            :class="state.current == 3 ? 'current' : ''"></div>
        <div class="ap drop4" @click="SwitchView(HomeContent.Default.ViewData.value[3].Id)"
            :class="state.current == 4 ? 'current' : ''"></div>
        <div class="ap drop5" @click="SwitchView(HomeContent.Default.ViewData.value[4].Id)"
            :class="state.current == 5 ? 'current' : ''"></div>
        <div class="ap drop6" @click="SwitchView(HomeContent.Default.ViewData.value[5].Id)"
            :class="state.current == 6 ? 'current' : ''"></div>
        <div class="ap drop7" @click="SwitchView(HomeContent.Default.ViewData.value[6].Id)"
            :class="state.current == 7 ? 'current' : ''"></div>
        <div class="ap drop8" @click="SwitchView(HomeContent.Default.ViewData.value[7].Id)"
            :class="state.current == 8 ? 'current' : ''"></div>
        <div class="ap drop9" @click="SwitchView(HomeContent.Default.ViewData.value[8].Id)"
            :class="state.current == 9 ? 'current' : ''"></div>
        <div class="ap drop10" @click="SwitchView(HomeContent.Default.ViewData.value[14].Id)"
            :class="state.current == 10 ? 'current' : ''"></div>
        <div class="ap drop11" @click="SwitchView(HomeContent.Default.ViewData.value[9].Id)"
            :class="state.current == 11 ? 'current' : ''"></div>
        <div class="ap drop12" @click="SwitchView(HomeContent.Default.ViewData.value[10].Id)"
            :class="state.current == 12 ? 'current' : ''"></div>
        <div class="ap drop13" @click="SwitchView(HomeContent.Default.ViewData.value[11].Id)"
            :class="state.current == 13 ? 'current' : ''"></div>
        <div class="ap drop14" @click="SwitchView(HomeContent.Default.ViewData.value[12].Id)"
            :class="state.current == 14 ? 'current' : ''"></div>
        <div class="ap drop15" @click="SwitchView(HomeContent.Default.ViewData.value[13].Id)"
            :class="state.current == 15 ? 'current' : ''"></div> -->
      <div v-for="(item, index) in HomeContent.Default.ViewData.value" @click="SwitchView(item.Id, index)" :key="index">
        <anchorPoint :Selected="HomeContent.Default.currentPoint.value" :IndexId="index"
          :ViewportPointX="item.Content.split(',')[0] + '%'" :ViewportPointY="item.Content.split(',')[1] + '%'">
        </anchorPoint>
      </div>
      <!-- <div class="ap drop16" :class="state.current == 15 ? 'current' : ''"></div> -->

      <!-- 整车线束导航部分  v-if="list.length > 0"-->

      <!-- <div class="navigation" @click="showNavigationLine">
      <button>整车线束导航</button>
    </div> -->
    </div>
  </div>
  <div class="showMap" v-else>
    <div class="controlMap">
      <img src="/public/images/ic_menu_收起.png" @click="isShowMap" />
      <div>导航视图</div>
    </div>
  </div>
  <!--  v-drag.offset.clamp  drag-trigger-->
  <div class="popup" v-if="HomeContent.Default.WindowViewType.value" v-drag.offset.clamp>
    <!-- 标题部分 -->
    <div class="titBox" drag-trigger>
      器件视角导航
      <close-outlined class="cancel" @click="close_btn" />
    </div>
    <!-- 搜索框 -->
    <a-input-search
      class="input"
      v-model:value="inputValue"
      placeholder="请输入器件代号"
      style="
        width: 300px;
        margin: 15px 0 15px 0;
        position: relative;
        text-align: center;
        text-transform: uppercase;
      " @search="search" custom-style="text-aline: center" />
    <button class="reset" @click="reset">重置</button>
    <div class="navigationType">
      <span>保险丝:{{ Fuse }}</span>
      <span>继电器:{{ Relay }}</span>
      <span>线束插头:{{ Device }}</span>
    </div>
    <!-- 历史点击记录 -->
    <text class="record" v-if="targetList.size > 0">历史记录</text>
    <div class="history">
      <div class="div" v-for="(item, index) in targetList" :key="index" @click="getHistoryInfo(item[0])">
        {{ item[1] }}
      </div>
    </div>
    <!-- 内容部分 -->
    <div class="content">
      <table>
        <tbody>
          <tr>
            <td @click="getInfo(item.ViewId, item.Device)" v-for="item in list">
              {{ item.Device }}
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>

  <!-- 倒计时组件 -->
  <!-- <TimeCountDown ref="countDown" v-if="
    HomeContent.Default.ModeType.value == 2 ||
    HomeContent.Default.ModeType.value == 3 ||
    HomeContent.Default.ModeType.value == 4
  "></TimeCountDown> -->
  <TimeCountDown ref="countDown"></TimeCountDown>
</template>

<script setup lang="ts">
import TimeCountDown from "../TimeCountDown/index.vue";
import { CloseOutlined } from "@ant-design/icons-vue";
import { ClientMessage } from "@runtime/generate/message/ClientMessage";
import WebSocketComponent from "@runtime/module/WebSocket/WebSocketComponent";
import {
  reactive,
  ref,
  toRefs,
  onBeforeMount,
  onMounted,
  onBeforeUpdate,
  onUpdated,
  onUnmounted,
  StyleValue,
} from "vue";
import HomeContent from "../HomeContent";
import MessageCode from "../MessageCode";
import anchorPoint from "./anchorPoint.vue";
import OrbitCameraComponent from "../OrbitCameraComponent";
import { ElMessageBox } from "element-plus";
import { text } from "stream/consumers";
import { number } from "echarts";
import { Item } from "ant-design-vue/lib/menu";

const Fuse = ref(0)
const Relay = ref(0)
const Device = ref(0)

const countDown = ref();
const state = reactive({
  current: 0,
});
async function SwitchView(Id: any, index: any) {
  HomeContent.Default.MenuData.value = {};
  HomeContent.Default.currentPoint.value = `${index}`;
  let newArr: any = await WebSocketComponent.Default.PostMessageAsync(
    new ClientMessage.DiagnosisAdapterRequest(
      MessageCode.Default.MsgCodesList.VIEW_DATA_CONFIG,
      `${Id}`
    )
  );
  console.log("ValData111111111111111111111111", JSON.parse(newArr.Content).Result);
  let ValData = JSON.parse(newArr.Content).Result;

  HomeContent.Default.visualAngle.value = ValData.Type;
  HomeContent.Default.roam.value = false;
  if (HomeContent.Default.visualAngle.value != "") {
    await WebSocketComponent.Default.PostMessageAsync(
      new ClientMessage.RoamModeRequest(false)
    );
  }
  await OrbitCameraComponent.Default.UpdateV2Async(
    ValData.TargetContainer,
    ValData.Horizontal,
    ValData.Vertical,
    ValData.Distance,
    ValData.MinDistance,
    ValData.MaxDistance,
    ValData.VerticalAngleMax,
    ValData.VerticalAngleMin,
    ValData.VerticalAngleSpeed,
    ValData.DistanceSpeed,
    ValData.HorizontalAngleSpeed,
    ValData.HorizontalAngleMax,
    ValData.HorizontalAngleMin
  );
}
function mouseenter() {
  WebSocketComponent.Default.PostMessage(
    new ClientMessage.DiagnosisDisplayUIMenuRequest(HomeContent.Default.tip.value)
  );
  HomeContent.Default.tip.value = false;
}
function mouseleave() {
  WebSocketComponent.Default.PostMessage(
    new ClientMessage.DiagnosisDisplayUIMenuRequest(HomeContent.Default.tip.value)
  );
  HomeContent.Default.tip.value = true;
}

const showDialog = ref(true);
function isShowMap() {
  showDialog.value = true;
}
function unShowMap() {
  showDialog.value = false;
}
const list: any = ref([]);
const searchList: any = ref([]);
const inputValue = ref("");
const AngleofViewType = ref(false);
const targetList = ref(new Map());
const IdList: any = ref([]);
onMounted(async () => {
  getAllLine();
  // 一进来这个页面就执行倒计时函数
  countDown.value.startCountdown();
});
// 获取所有的视角导航点
async function getAllLine() {
  let newArr: any = await WebSocketComponent.Default.PostMessageAsync(
    new ClientMessage.DiagnosisAdapterRequest(
      MessageCode.Default.MsgCodesList.DIAGNOSTIC_DEVICE_GET_NAVIGATION
    )
  );
  console.log('@@@@@@@@@@@@', JSON.parse(newArr.Content));
  list.value = JSON.parse(newArr.Content).Result;
  console.log('list.value', list.value);
  Fuse.value = 0
  Relay.value = 0
  Device.value = 0
  list.value.forEach((element: any) => {
    if (element.Type == 'Fuse') {
      Fuse.value = Fuse.value + 1
    } else if (element.Type == 'Relay') {
      Relay.value = Relay.value + 1
    } else if (element.Type == 'Device') {
      Device.value = Device.value + 1
    }
  });

}
function showNavigationLine() {
  HomeContent.Default.WindowViewType.value = true;
  if (inputValue.value) {
    list.value = list.value.filter((item: any) => {
      return item.Device.includes(inputValue.value);
    });
  } else {
    getAllLine();
  }
  HomeContent.Default.MenuData.value = {};
}
function close_btn() {
  // showDialog.value = false;
  HomeContent.Default.WindowViewType.value = false;
}
async function getHistoryInfo(Id: number) {
  let newArr: any = await WebSocketComponent.Default.PostMessageAsync(
    new ClientMessage.DiagnosisAdapterRequest(
      MessageCode.Default.MsgCodesList.VIEW_DATA_CONFIG,
      `${Id}`
    )
  );
  let ValData = JSON.parse(newArr.Content).Result;
  console.log('ValData', ValData);
  if (HomeContent.Default.roam.value) {
    HomeContent.Default.roam.value = false;
    await WebSocketComponent.Default.PostMessageAsync(
      new ClientMessage.RoamModeRequest(HomeContent.Default.roam.value)
    );
  }

  await WebSocketComponent.Default.PostMessageAsync(
    new ClientMessage.OrbitCameraUpdateAngleRequest(
      ValData.TargetContainer,
      ValData.Horizontal,
      ValData.Vertical,
      ValData.Distance,
      ValData.MinDistance,
      ValData.MaxDistance,
      ValData.VerticalAngleMax,
      ValData.VerticalAngleMin,
      ValData.VerticalAngleSpeed,
      ValData.DistanceSpeed,
      ValData.HorizontalAngleSpeed,
      ValData.HorizontalAngleMax,
      ValData.HorizontalAngleMin
    )
  );
  close_btn();
}
async function getInfo(Id: number, Name: String) {
  let newArr: any = await WebSocketComponent.Default.PostMessageAsync(
    new ClientMessage.DiagnosisAdapterRequest(
      MessageCode.Default.MsgCodesList.VIEW_DATA_CONFIG,
      `${Id}`
    )
  );
  let ValData = JSON.parse(newArr.Content).Result;



  if (HomeContent.Default.roam.value) {
    HomeContent.Default.roam.value = false;
    await WebSocketComponent.Default.PostMessageAsync(
      new ClientMessage.RoamModeRequest(HomeContent.Default.roam.value)
    );
  }

  await WebSocketComponent.Default.PostMessageAsync(
    new ClientMessage.OrbitCameraUpdateAngleRequest(
      ValData.TargetContainer,
      ValData.Horizontal,
      ValData.Vertical,
      ValData.Distance,
      ValData.MinDistance,
      ValData.MaxDistance,
      ValData.VerticalAngleMax,
      ValData.VerticalAngleMin,
      ValData.VerticalAngleSpeed,
      ValData.DistanceSpeed,
      ValData.HorizontalAngleSpeed,
      ValData.HorizontalAngleMax,
      ValData.HorizontalAngleMin
    )
  );

  targetList.value.set(Id, Name);

  // 将Map对象转换为数组
  const myArray = Array.from(targetList.value);

  // 如果数组长度超过3，则删除第一个元素
  if (myArray.length > 3) {
    myArray.shift();
  }

  // 将数组转换回Map对象
  targetList.value = new Map(myArray);
  close_btn();
}

function search() {
  searchList.value = list.value.filter((item: any) => {
    // return item.Device.match(/inputValue.value/g); // 区分大小写
    // return item.Device.includes(inputValue.value);// 区分大小写
    // return item.Device.includes(inputValue.value.toUpperCase()); // 转为大写
    // if (item.Device == inputValue.value) {
    //   return item;
    // }
    // 不区分大小写的，转大写+转小写，合并数组

    // let patt = new RegExp(inputValue.value, "i");
    // let result = patt.test(item.Device);
    // if (result) {
    //   return item.Device;
    // }
    if (item.Device.toLowerCase().includes(inputValue.value.toLowerCase())) {
      return item.Device;
    }
  });

  if (!inputValue.value) {
    ElMessageBox.alert("请输入搜索内容", "提示", {
      confirmButtonText: "确认",
      center: true,
    });
  } else if (inputValue.value && searchList.value.length > 0) {
    list.value = searchList.value;
  } else if (inputValue.value && searchList.value.length == 0) {
    ElMessageBox.alert("没有此搜索项", "提示", {
      confirmButtonText: "确认",
      center: true,
    });
    return list.value;
  }

  // inputValue.value = ''
}
function reset() {
  getAllLine();
  inputValue.value = "";
}
</script>

<style scoped>
.input>>>.ant-input-group .ant-input {
  height: 48px;
  width: 304px;
}

.input>>>.ant-input-search-button {
  width: 48px;
  height: 48px;
}

.input>>>.anticon svg {
  font-size: 20px;
}

.reset {
  position: absolute;
  width: 80px;
  height: 48px;
  top: 99px;
  left: 400px;
  border: 1px solid #e9e9e9;
  background-color: #f8f8f8;
  border-radius: 3px;
}

.navigationType {
  width: 1306px;
  height: 41px;
  border-bottom: 2px solid #f7f7f7;
  font-size: 20px;
  color: #717171;
}

.navigationType>span {
  margin-right: 20px;
}

.history {
  width: 550px;
  height: 48px;
  text-align: center;
  line-height: 33px;
  background-color: #fff;
  color: #000000;
  position: absolute;
  right: 92px;
  top: 100px;
  display: flex;
  font-weight: 500;
}

.record {
  font-size: 16px;
  font-weight: 400;
  position: absolute;
  top: 97px;
  right: 666px;
}

.div {
  width: 170px;
  height: 48px;
  line-height: 48px;
  font-size: 16px;
  border: 1px solid #dddddd;
  background: #f8f8f8;
  border-radius: 8px 8px 8px 8px;
}

.div:nth-child(1) {
  margin-right: 20px;
}

.div:nth-child(2) {
  margin-right: 20px;
}

.viewBox {
  background-color: #ffffff;
  padding: 0px 7px 6px 7px;
  height: 440px;
  width: 338px;
  position: absolute;
  right: 9px;
  top: 75px;
  border-radius: 10px;
}

.showMap {
  background-color: #ffffff;
  width: 130px;
  position: absolute;
  right: 9px;
  top: 75px;
  padding: 0 20px;
  border-radius: 10px;
}

.controlMap {
  display: flex;
  color: black;
  font-size: 15px;
  margin: 13px 0;
}

.map {
  position: absolute;
  /* right: 0; */
  /* top: 100px; */
  width: 324px;
  height: 384px;
  /* background-image: url("/images/Map_bg.png"); */
  /* background-image: url("/res/Map/Map_bg.png"); */
  background-image: url("/res/Map/导航视图.png");
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

.navigation {
  position: relative;
  top: 384px;
  background-color: #fff;
}

button {
  color: black;
  width: 324px;
  height: 50px;
  /* border-style: none; */
}

.popup {
  position: absolute;
  width: 1440px;
  height: 814px;
  background-color: #fff;
  top: 50%;
  left: 50%;
  padding-left: 40px;
  padding-right: 20px;
  border-radius: 16px 16px 16px 16px;
  box-shadow: 0 0 20px #666;
  transform: translate(-50%, -50%);
  z-index: 1;
}

.title {
  display: flex;
  height: 50px;
}

.ico_close {
  color: black;
  font-size: 18px;
  position: relative;
  line-height: 50px;
  left: 680px;
}

text {
  line-height: 50px;
  color: black;
  font-size: 25px;
}

.content {
  height: 550px;
  overflow: auto;
  margin-top: 40px;
}

td {
  display: inline-block;
  width: 312px;
  height: 59px;
  font-size: 18px;
  font-weight: 500;
  text-align: center;
  line-height: 59px;
  border: 1px solid #dddddd;
  background-color: #fff;
  margin-right: 20px;
  margin-bottom: 10px;
  color: black;
  border-radius: 8px 8px 8px 8px;
}

td:hover {
  background: linear-gradient(90deg,
      #9bfb4e 0%,
      rgba(155, 251, 78, 0.75) 58%,
      rgba(155, 251, 78, 0) 100%);
  border-color: rgb(161, 241, 95);
}

.div:hover {
  background: linear-gradient(90deg,
      #9bfb4e 0%,
      rgba(155, 251, 78, 0.75) 58%,
      rgba(155, 251, 78, 0) 100%);
  border-color: rgb(161, 241, 95);
}

::-webkit-scrollbar {
  width: 6px;
  height: 1px;
}

::-webkit-scrollbar-thumb {
  border-radius: 10px;
  background: #dfe1e8;
}

::-webkit-scrollbar-track {
  border-radius: 10px;
  background: #f8f8f8;
}

.titBox {
  height: 54px;
  font-size: 30px;
  font-weight: 600;
  color: #000000;
  line-height: 42px;
  margin-top: 30px;
  margin-left: 10px;
}

.cancel {
  font-size: 28px;
  font-weight: 600;
  float: right;
  margin-top: 4px;
  margin-right: 8px;
}

.ap {
  position: absolute;
  width: 32px;
  height: 32px;
  background-image: url("/images/ic_定位.png");
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

.ap:hover {
  background-image: url("/images/ic_定位_当前位置.png");
}

.current {
  background-image: url("/images/ic_定位_当前位置.png");
}

/*
.drop1 {
    left:52.5%;
    top: 20%;
}

.drop2 {
    left: 77%;
    top: 36.5%;
}

.drop3 {
    left: 80%;
    top: 8%;
}

.drop4 {
    left: 55.6%;
    top: 8%;
}

.drop5 {
    left: 28%;
    top: 47%;
}

.drop6 {
    left: 15.5%;
    top: 47%;
}

.drop7 {
    left: 3%;
    top: 21%;
}

.drop8 {
    left: 12%;
    top: 16%;
}

.drop9 {
    left: 17%;
    top: 82%;
}

.drop10 {
    left: 53%;
    top: 39%;
}

.drop11 {
    left: 53%;
    top: 73%;
}
.drop12 {
    left: 40%;
    top: 44%;
}
.drop13{
    left: 65.5%;
    top: 44%;
}
.drop14{
    left: 65.5%;
    top: 55%;
}
.drop15{
    left: 40%;
    top: 55%;
} */
/* 充电枪视角 */
/* .drop15 {
    left: 76%;
    top: 64%;
} */
</style>
